﻿using Ndns.Packet.Parser.Helpers;


namespace System.Buffers
{
    static class ArrayPoolLeaseExtensions
    {
        public static ArrayPoolLease<T> Lease<T>(this ArrayPool<T> pool, int minimumLength, bool clearArrayOnReturn = false)
        {
            if (pool == null)
            {
                throw new ArgumentNullException(nameof(pool));
            }

            var buffer = pool.Rent(minimumLength);
            return new ArrayPoolLease<T>(pool, buffer, clearArrayOnReturn);
        }

        public static ArrayPoolLease<T> LeaseOrAllocate<T>(this ArrayPool<T> pool, int minimumLength, bool clearArrayOnReturn = false)
        {
            if (pool == null)
            {
                return new ArrayPoolLease<T>(new T[minimumLength]);
            }
            var buffer = pool.Rent(minimumLength);
            return new ArrayPoolLease<T>(pool, buffer, clearArrayOnReturn);
        }
    }
}
